﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Operations_Views_MyDocumentsView : BaseHostedView
{
    #region Fields

    private IIndexingService indexingService;

    #endregion

    #region Properties

    /// <summary>
    /// Sets initialized indexing service instance. This property is utilized by 
    /// the IoC container for supplying this view with indexing service.
    /// </summary>
    public IIndexingService IndexingService
    {
        set
        {
            this.indexingService = value;
        }
    }

    #endregion

    #region Event handlers

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.documentsGridEX.DataSource = this.indexingService.GetDocuments(Page.User.Identity.Name);
            this.documentsGridEX.DataBind();
        }
        catch (Exception ex)
        {
            base.Navigate(UsersManagementViews.ErrorView, ReturnViewName, ex);
        }
    }

    protected void ShowDocumentDetails(object sender, EventArgs e)
    {
        base.Navigate(DocumentsViews.DocumentDetailsView, DocumentsViews.MyDocumentsView, GetSelectedDocument());
    }

    #endregion

    #region Support methods

    private Document GetSelectedDocument()
    {
        int documentId = (int)documentsGridEX.GetRow().Cells["Id"].Value;
        return (from document in documentsGridEX.DataSource as IEnumerable<Document>
                where (document.Id.HasValue) && (document.Id.Value == documentId)
                select document).FirstOrDefault();
    }

    #endregion
}
